<?php
//this script accepts a table name and entry id number by POST and spits back the row values

//I suppose it should be made secure in various ways?
//TODO: secure POST requests!

include('db_and_modx_init.php');
include "core.inc.php";
include "make_forms.php";

global $replace_richtexteditor;

$table = mysql_real_escape_string($_POST['table']); //is escape here really necessary?  Better safe than sorry.
$id = mysql_real_escape_string($_POST['entryid']);
$current_doc_id = $_POST['current_doc_id'];
$plugin_cell = stripslashes($_POST['plugin_cell']);


//exampele:
//function df_make_form($table_info, $current_doc_id, $current_record_id=''){
//$contacts = new df_table_info('office_contacts', array('office_id' => '22'), preg_split('/ /', 'name email phone status') );

// $fkeys = preg_replace("/,/", '', stripslashes($_POST['foreign_keys']), 1);  //remove first comma
// $table_info = new df_table_info($table, json_decode($fkeys, true));
$table_info = unserialize(stripslashes($_POST['table_info']));



//need to set for tmpvars.inc.php
$base_url = $modx->config['base_url'];
$rb_base_url = $modx->config['rb_base_url'];
$manager_theme = $modx->config['manager_theme'];

df_make_form($table_info, $current_doc_id, 'edit', $id, $plugin_cell);
// echo "texteditors::".print_r($replace_richtexteditor); //todo: handle any rich text editor...
//height is being obstinate..
echo '<script type="text/javascript">
tinyMCE.init({
	  theme : "simple",
	  mode : "exact",
	  plugins : "paste",
	  paste_remove_styles: true,
	  relative_urls : true,
	  document_base_url : "",
	  remove_script_host : true,
	  language : "en",
	  elements : "tvedit_content",
	  valid_elements : tinymce_valid_elements,
	  extended_valid_elements : tinymce_extended_valid_elements,
	  invalid_elements : tinymce_invalid_elements,
	  content_css : "/css/default-tinyMCE.css",
	  entity_encoding : "named",
	  cleanup: true,
	  apply_source_formatting : true,
	  remove_linebreaks : false,
	  convert_fonts_to_spans : "true",
	  button_tile_map : false 

});

tinyMCE.execCommand("mceAddControl", false, "tvedit_content");
j("#tvedit_content").attr("width", "500px").attr("height", "400px");  

</script>';

$summary_fieldNames = array();

foreach ($table_info->getSummaryFields() as $col => $title){
	$summary_fieldNames[] = 'tvedit_'.$col;
}

// echo "<div style='display:none;' id='summary_fieldNames' name='summary_fieldNames'>".json_encode($summary_fieldNames)."</div>";
echo "<div style='display:none;' id='table_info' name='table_info>".serialize($table_info)."</div>";
echo "<div style='display:none;' id='plugin_cell' name='plugin_cell>".$plugin_cell."</div>";


?>